package com.jf.springcloud.util;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-02-14
 * Time:21:45
 */
@Data
public class Msg {
    private String message;
    private int code;
    @Setter(AccessLevel.NONE)
    private Map<String, Object> extend;

    public static Msg success() {
        return custom(200, "success");
    }

    public static Msg success(String message) {
        return custom(200, message);
    }

    public static Msg success(int code, String message) {
        return custom(code, message);
    }
    public static Msg failed() {
        return custom(400, "failed");
    }
    public static Msg failed(String message) {
        return custom(400, message);
    }
    public static Msg failed(int code, String message) {
        return custom(code, message);
    }
    private static Msg custom(int code, String message) {
        Msg msg = new Msg();
        msg.code = code;
        msg.message = message;
        return msg;
    }
    public Msg addData(String key, Object value) {
        if (extend == null) {
            extend = new HashMap<>();
        }
        extend.put(key, value);
        return this;
    }
    public <T> void addData(Map<String, T> data) {
        if (extend == null) {
            extend = new HashMap<>();
        }
        extend.putAll(data);
    }
}
